package cate.game.pvp.gradepvp;

import cate.common.table.d.GDGradePvp;

public class GradePvpPeriod {
    public static GradePvpPeriod NOT_OPEN;
    public static GradePvpPeriod FIRST_HALF;
    public static GradePvpPeriod FIRST_HALF_SETTLE;
    public static GradePvpPeriod SECOND_HALF;
    public static GradePvpPeriod SECOND_HALF_SETTLE;
    protected static GradePvpPeriod[] statuses;

    private long startTime;
    private long endTime;
    private int ordinal;
    private String nameDesc;

    public static void initialize(){
        NOT_OPEN = new GradePvpPeriod(
                "NOT_OPEN",
                0,
                0,
                0);
        FIRST_HALF = new GradePvpPeriod(
                "FIRST_HALF",
                GDGradePvp.FIRST_HALF_START_TIME,
                GDGradePvp.FIRST_HALF_END_TIME,
                1);
        FIRST_HALF_SETTLE = new GradePvpPeriod(
                "FIRST_HALF_SETTLE",
                GDGradePvp.FIRST_HALF_SETTLE_START_TIME,
                GDGradePvp.FIRST_HALF_SETTLE_END_TIME,
                2);
        SECOND_HALF = new GradePvpPeriod(
                "SECOND_HALF",
                GDGradePvp.SECOND_HALF_START_TIME,
                GDGradePvp.SECOND_HALF_END_TIME,
                3);
        SECOND_HALF_SETTLE = new GradePvpPeriod(
                "SECOND_HALF_SETTLE",
                GDGradePvp.SECOND_HALF_SETTLE_START_TIME,
                GDGradePvp.SECOND_HALF_SETTLE_END_TIME,
                4);
        statuses = new GradePvpPeriod[]{
                NOT_OPEN,
                FIRST_HALF,
                FIRST_HALF_SETTLE,
                SECOND_HALF,
                SECOND_HALF_SETTLE};
    }

    public static GradePvpPeriod[] values(){
        return statuses;
    }

    public GradePvpPeriod(String nameDesc, long startTime, long endTime, int ordinal) {
        this.nameDesc = nameDesc;
        this.startTime = startTime;
        this.endTime = endTime;
        this.ordinal = ordinal;
    }

    public int ordinal(){
        return this.ordinal;
    }

    public static GradePvpPeriod getCurrentStatus(long seasonContinueTime) {
        for (GradePvpPeriod status : GradePvpPeriod.values()) {
            if (seasonContinueTime >= status.startTime && seasonContinueTime <= status.endTime) {
                return status;
            }
        }
        return null;
    }

    public static GradePvpPeriod getNextStatus(GradePvpPeriod status) {
        if (status == null) {
            return GradePvpPeriod.NOT_OPEN;
        }
        if(status.equals(SECOND_HALF_SETTLE)){
            return GradePvpPeriod.FIRST_HALF;
        }
        return GradePvpPeriod.values()[status.ordinal() + 1];
    }

    @Override
    public boolean equals(Object obj) {
        if(this == obj){
            return true;
        }
        if(!(obj instanceof GradePvpPeriod)){
            return false;
        }
        GradePvpPeriod that = (GradePvpPeriod) obj;
        return this.ordinal == that.ordinal;
    }

    @Override
    public int hashCode() {
        int result = 17;
        result = 31 * result + (int)(startTime ^ (startTime >>> 32));
        result = 31 * result + (int)(endTime ^ (endTime >>> 32));
        result = 31 * result + ordinal;
        return result;
    }

    @Override
    public String toString(){
        return this.nameDesc;
    }

    public long getStartTime() {
        return this.startTime;
    }

    public long getEndTime() {
        return this.endTime;
    }
}
